package com.monkeyboy.demo.base.producer;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;

import java.util.concurrent.TimeUnit;

/**
 * 发送同步消息
 *
 * @Author Gavin
 * @date 2020.08.18 12:10
 */
public class SyncProducer {
    public static void main(String[] args) throws Exception {
//        1.创建消费者Consumer，制定消费者组名,唯一
        DefaultMQProducer producer = new DefaultMQProducer("group1");
//        2.指定Nameserver地址
        producer.setNamesrvAddr("192.168.1.140:9876;192.168.1.141:9876");
//        3.启动producer
        producer.start();
        for (int i = 0; i <10 ; i++) {
            //4.创建消息对象，指定主题Topic,Tag和消息体
            /**
             * 参数一：消息主题Topic(相当于就是消息的类别);
             * 参数二：消息Tag(小类别)
             * 参数三：消息内容
             */
            Message msg=new Message("base","Tag1",("Hello World"+i).getBytes());
            SendResult result = producer.send(msg);//同步消息发送并获取到返回的结果
            //发送状态
            SendStatus sendStatus = result.getSendStatus();//发送状态
            String msgId = result.getMsgId();//消息ID
            int queueId = result.getMessageQueue().getQueueId();//消息接受队列ID
            System.out.println("发送结果："+result);
            TimeUnit.SECONDS.sleep(1);
        }
        //关闭生产者producer
        producer.shutdown();
    }
}
